<?php
/*
* Author：鑫宇工作室
* QQ：639519801
* url：www.xinyuream.com
*/
include_once $_SERVER['DOCUMENT_ROOT'].'/func/include.php';
$user=admin_check(true);
$id=intval(input('id'));
$u=false;
if($id>0){
	$re=query("select * from xy_app where id='{$id}'");
	if($re){
		$u=$re[0];
	}
}
?>
<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <input type="hidden" name="id" value="<?=$u['id']?>">
            <label class="layui-form-label required">软件名称</label>
            <div class="layui-input-block">
                <input type="text" name="appname" lay-verify="required" lay-reqtext="软件名称" placeholder="请输入软件名称" value="<?=$u?$u['appname']:''?>" class="layui-input">
            </div>
        </div>
		<div class="layui-form-item">
            <label class="layui-form-label <?=$u?'':'required'?>">版本号</label>
            <div class="layui-input-block">
                <input type="text" name="version" <?=$u?'':'lay-verify="required"'?> lay-reqtext="版本号不能为空" placeholder="请输入版本号" value="<?=$u?$u['version']:''?>" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label <?=$u?'':'required'?>">最新地址</label>
            <div class="layui-input-block">
                <input type="text" name="newurl"  lay-reqtext="最新版下载地址" placeholder="请输入最新版下载地址" value="<?=$u?$u['newurl']:''?>" class="layui-input">
                <div>
                    <button class="layui-btn layui-btn-xs layui-btn-normal" lay-submit lay-filter="saveApp" >本地上传</button>
                    <span class="jindu"></span>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label <?=$u?'':'required'?>">更新公告</label>
            <div class="layui-input-block">
                <textarea type="text" name="new_notice"  lay-reqtext="请填写公告" placeholder="更新软件时显示的公告"  class="layui-input"><?=$u?$u['new_notice']:''?></textarea>
            </div>
        </div>

		<div class="layui-form-item">
            <label class="layui-form-label ">是否启用</label>
            <div class="layui-input-block">
               <input type="checkbox" <?=$u?($u['status']==1?'checked=""':''):'checked=""'?> name="status" lay-skin="switch" lay-filter="switchTest" lay-text="启用|禁用">
            </div>
        </div>

        <input type="file" style="position: absolute;top: -100px;left: -100px;"
               class="fileBox" id="fileBox" name="app" value="" onchange="addApp()">

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script>
    var lay,$_;
    layui.use(['form', 'table','laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
			laydate =layui.laydate
            $ = layui.$;
        lay = layer
        $_=$;
        form.render();
		laydate.render({
		  elem: '#date', //元素的id
		  type:'datetime'
		});
        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data.field.status=data.field.status=='on'?1:2;
			$.post({
				url:'/admin?ac=<?=$u?'editapp':'addapp'?>',
				data: data.field,
				type:'post',
				dataType:'json',
				success:function(e){
					layui.exit(e);
					if(e.code===200){
						layer.msg(e.msg,{icon: 6,time: 2000});
						parent.layui.table.reload('datalist');
						layer.close(parentIndex);
					}else{
						layer.msg(e.msg,{icon: 5,time: 2000});
					}
				},
				error: function(e){
				  layer.msg('数据异常，操作失败！',{icon: 5,time: 2000});
				  
				}
			});

            return false;
        });

        form.on('submit(saveApp)', function (data) {
            $('.fileBox').val('');
            $('.fileBox').click();
            $('input[name="newurl"]').val('');
            return false;
        });
    });

    function addApp(){
        var fromObj=new FormData();
        var picObj=$("#fileBox")[0].files;
        //循环像from表单对象里面添加 图片数据
        fromObj.append('appFile',picObj[0]);
        fromObj.append('filename','appFile');
        fromObj.append('appid','<?=$u['id']?>');
        $.ajax({
            url:'/admin?ac=appAdd',
            type:'POST',
            data:fromObj,
            contentType:false,//如果是传图片则这俩项需要为false
            processData:false,
            success:function(e){
                if(e.code==200){
                    lay.msg(e.msg,{icon:1})
                    $('input[name="newurl"]').val(e.data)
                }else{
                    lay.msg(e.msg,{icon:5})
                }
            },
            error:function(e){
                lay.msg('上传失败');
                console.log(e,'---上传错误');
            },
            xhr:xhrFunc,
        })
    }
    function xhrFunc(){
        var myXhr = $.ajaxSettings.xhr();
        if(myXhr.upload){ //检查upload属性是否存在
            //绑定progress事件的回调函数
            $('#progressId').text(); //清空
            myXhr.upload.addEventListener('progress', function(e)
                {
                    if (e.lengthComputable)
                    {
                        var percent = "" + (e.loaded/e.total*100).toFixed(2) + "%";
                        $('.jindu').text(percent);
                    }
                },
                false);
        }
        return myXhr; //xhr对象返回给jQuery使用
    }



</script>